package com.hqyj.am.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.am.dao.UserMapper;
import com.hqyj.am.dao.VacationMapper;
import com.hqyj.am.dao.WorkNoteMapper;
import com.hqyj.am.pojo.*;
import com.hqyj.am.dao.ScheduleMapper;
import com.hqyj.am.service.IScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 系统设置类
 *
 * @author rock
 * @since 2022-08-16
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements IScheduleService {

    @Resource
    public WorkNoteMapper workNoteMapper;

    @Resource
    public ScheduleMapper scheduleMapper;

    @Resource
    public UserMapper userMapper;

    @Resource
    public VacationMapper vacationMapper;

    /**
     * 查询每月请假次数最多的员工
     * @return
     */
    @Override
    public ResultVo selectLeaveMax(String date1, String date2) {
        //计算每个人每月的请假次数
        QueryWrapper<Vacation> qw = new QueryWrapper<>();
        qw.select("user_id, count(user_id) as vacation_id")
          .ge("start_time", date1)
          .le("end_time", date2)
          .eq("vacation_no", "通过")
          .groupBy("user_id")
          .orderByDesc("vacation_id");
        List<Vacation> vacationList = vacationMapper.selectList(qw);
        ResultVo rv = new ResultVo();
        //找到请假次数最多的员工
        if(vacationList.size()==0){
            rv.setInfo("无请假次数最多的员工");
            return rv;
        }
        int sum = vacationList.get(0).getVacationId();
        List<Integer> userList = new ArrayList<>();
        for(int i=0; i<vacationList.size() && vacationList.get(i).getVacationId()==sum; i++){
            userList.add(vacationList.get(i).getUserId());
        }
        rv.setData(userList);
        List<String> list=new LinkedList<>();
        for (Integer ls : userList) {
            QueryWrapper<User> wrapper  = new QueryWrapper<>();
            boolean isLs = ls!=null;
            wrapper.eq(isLs,"user_id",ls);
            User user=userMapper.selectOne(wrapper);
            list.add(user.getUserName());
        }
        rv.setData(list);
        rv.setSuccess(true);
        rv.setInfo("查询成功");
        return rv;
    }

    /**
     * 查询每月迟到最多的员工
     * @return
     */
    @Override
    public ResultVo selectLateMax(String date1, String date2) {
        //计算每个人每月迟到的次数
        QueryWrapper<WorkNote> qw = new QueryWrapper<>();
        qw.select("user_id, sum(late) as late")
          .between("date", date1, date2)
          .groupBy("user_id")
          .orderByDesc("late");
        List<WorkNote> workNoteList = workNoteMapper.selectList(qw);
        ResultVo rv = new ResultVo();
        if(workNoteList.size()==0){
            rv.setInfo("无迟到次数最多的员工");
            return rv;
        }
        int sum = workNoteList.get(0).getLate();
        //找到迟到最多的员工
        List<Integer> userList = new ArrayList<>();
        for(int i=0; i<workNoteList.size() && workNoteList.get(i).getLate()==sum; i++){
            userList.add(workNoteList.get(i).getUserId());
        }
        rv.setData(userList);
        List<String> list=new LinkedList<>();
        for (Integer ls : userList) {
            QueryWrapper<User> wrapper  = new QueryWrapper<>();
            boolean isLs = ls!=null;
            wrapper.eq(isLs,"user_id",ls);
            User user=userMapper.selectOne(wrapper);
            list.add(user.getUserName());
        }
        rv.setData(list);
        rv.setSuccess(true);
        rv.setInfo("查询成功");
        return rv;
    }

    /**
     * 查询每月早退最多的员工
     * @return
     */
    @Override
    public ResultVo selectEarlyMax(String date1, String date2) {
        //计算每个人每月早退的次数
        QueryWrapper<WorkNote> qw = new QueryWrapper<>();
        qw.select("user_id, sum(early) as early")
                .between("date", date1, date2)
                .groupBy("user_id")
                .orderByDesc("early");
        List<WorkNote> workNoteList = workNoteMapper.selectList(qw);
        ResultVo rv = new ResultVo();
        if(workNoteList.size()==0){
            rv.setInfo("无早退次数最多的员工");
            return rv;
        }
        int sum = workNoteList.get(0).getEarly();
        //找到早退最多的员工
        List<Integer> userList = new ArrayList<>();
        for(int i=0; i<workNoteList.size() && workNoteList.get(i).getEarly()==sum; i++){
            userList.add(workNoteList.get(i).getUserId());
        }
        rv.setData(userList);
        List<String> list=new LinkedList<>();
        for (Integer ls : userList) {
            QueryWrapper<User> wrapper  = new QueryWrapper<>();
            boolean isLs = ls!=null;
            wrapper.eq(isLs,"user_id",ls);
            User user=userMapper.selectOne(wrapper);
            list.add(user.getUserName());
        }
        rv.setData(list);
        rv.setSuccess(true);
        rv.setInfo("查询成功");
        return rv;
    }

    @Override
    public ResultVo updateSystem(Schedule schedule) {
        System.out.println(schedule);
        ResultVo rv = new ResultVo();
        if(schedule.getScheduleQuitTime()==null || schedule.getScheduleWorkTime()==null){
            rv.setInfo("非法输入，请重新设置！");
            return rv;
        }
        if(schedule.getScheduleWorkTime().isAfter(schedule.getScheduleQuitTime())){
            rv.setInfo("上班时间比下班时间晚，请重新输入！");
            return rv;
        }
        int num = scheduleMapper.updateById(schedule);
        if(num>0){
            rv.setInfo("修改成功");
            rv.setSuccess(true);
        }else {
            rv.setInfo("修改失败，请重新操作");
        }
        return rv;
    }

    @Override
    public ResultVo selectSystem(Schedule schedule) {
        Page<Schedule> page = new Page<>(schedule.getPage(),schedule.getRow());
        QueryWrapper<Schedule> qw = new QueryWrapper<>();
        Page<Schedule> pageList = scheduleMapper.selectPage(page, qw);
        System.out.println("每页集合：" + pageList.getRecords());
        System.out.println("总页数：" + pageList.getPages());
        System.out.println("总条数：" + pageList.getTotal());
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageList);
        map.put("totalPage", pageList.getPages());
        map.put("total", pageList.getTotal());
        ResultVo rv = new ResultVo();
        rv.setData(map);
        rv.setSuccess(true);
        return rv;
    }

    @Override
    public ResultVo test(int i) {
        ResultVo rv = new ResultVo();
        System.out.println(i);
        return rv;
    }
}
